#!/bin/sh
# PCP QA Test No. 1204
# pmlogctl - basic functionality
#
# see qa/1226 for the pmiectl variant of this test
#
# Copyright (c) 2020 Ken McDonell.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

if pmlogctl -c default status 2>/dev/null | grep ' default ' >/dev/null
then
    _notrun "at least one pmlogger already defined for \"default\" class"
fi

_cleanup()
{
    echo "_cleanup: ..." >>$seq_full
    cd $here
    $sudo pmlogctl -f -c default destroy localhost >>$seq_full 2>&1
    $sudo pmlogctl -a -f -c pmfind destroy localhost >>$seq_full 2>&1
    $sudo rm -rf $tmp $tmp.*
    $sudo rm -rf $PCP_ARCHIVE_DIR/localhost
    $sudo rm -rf $PCP_ARCHIVE_DIR/pmfind-localhost
    $sudo rm -rf $PCP_ARCHIVE_DIR/$seq-foo

}

status=1	# failure is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

_filter()
{
    tee -a $seq_full \
    | sed \
	-e '/^# created by pmlogctl/s/ on .*/ on DATE/' \
	-e "s;$tmp\.;TMP.;g" \
	-e "s;$PCP_BINADM_DIR/;PCP_BINADM_DIR/;g" \
	-e "s;$PCP_ARCHIVE_DIR/;PCP_ARCHIVE_DIR/;g" \
	-e "s;$PCP_TMP_DIR/;PCP_TMP_DIR/;g" \
	-e "s;$PCP_TMPFILE_DIR/pmlogctl\.[^/]*;PCP_TMPFILE_DIR/pmlogctl.XXXXX;g" \
	-e "s;$PCP_ETC_DIR/;PCP_ETC_DIR/;g" \
    # end
}

# Build filter for any existing non-qa and non-primary pmlogger instances.
# The "pmcd Host" and "Class" fields from the pmlogctl status output
# should suffice to uniquely identify each.
#
pmlogctl status \
| $PCP_AWK_PROG >$tmp.awk '
NR == 1	{ next }
NF >= 5	{ if ($3 == "primary") next
	  print "$1 == \"" $1 "\" && $3 == \"" $3 "\" { next }"
	}
END	{ print "{ print }" }'

# Note status command output order is non-deterministic, hence the sort
# at the end
#
_filter_status()
{
    _wait_pmlogctl >>$seq_full 2>&1
    tee -a $seq_full \
    | $PCP_AWK_PROG -f $tmp.awk \
    | sed >$tmp.tmp \
	-e "/^`hostname` .* primary /d" \
	-e 's/[ 	][ 	]*/ /g' \
	-e 's/2[0-9][0-9][0-9][01][0-9][0-3][0-9]\...\.[^ ]*/<archivename>/' \
	-e 's/ [0-9][0-9]* / <pid> /' \
    # end
    head -1 $tmp.tmp
    sed -e '1d' $tmp.tmp | LC_COLLATE=POSIX sort
}

# real QA test starts here

echo "== usage"
pmlogctl --help 2>&1 | _filter \
| sed \
    -e 's/[ 	][ 	]*/ /g' \
# end

echo
echo '== -N -V create -c default' | tee -a $seq_full
pmlogctl -N -V create -c default localhost 2>&1 | _filter

echo | tee -a $seq_full
echo '== create -c default' | tee -a $seq_full
$sudo pmlogctl create -c default localhost | _filter
echo '== status' | tee -a $seq_full
pmlogctl status | _filter_status
echo '== status localhost' | tee -a $seq_full
pmlogctl status localhost | _filter_status

echo | tee -a $seq_full
echo '== stop -c default' | tee -a $seq_full
$sudo pmlogctl stop -c default localhost | _filter
echo '== status' | tee -a $seq_full
pmlogctl status | _filter_status
echo '== status localhost' | tee -a $seq_full
pmlogctl status localhost | _filter_status

echo | tee -a $seq_full
echo '== start -c default' | tee -a $seq_full
$sudo pmlogctl start -c default localhost | _filter
echo '== status' | tee -a $seq_full
pmlogctl status | _filter_status
echo '== status localhost' | tee -a $seq_full
pmlogctl status localhost | _filter_status

echo | tee -a $seq_full
echo '== restart -c default' | tee -a $seq_full
$sudo pmlogctl restart -c default localhost | _filter
echo '== status' | tee -a $seq_full
pmlogctl status | _filter_status
echo '== status localhost' | tee -a $seq_full
pmlogctl status localhost | _filter_status

echo | tee -a $seq_full
echo '== destroy -c default' | tee -a $seq_full
$sudo pmlogctl destroy -c default localhost | _filter
echo '== status' | tee -a $seq_full
pmlogctl status | _filter_status
echo '== status localhost' | tee -a $seq_full
pmlogctl status localhost | _filter_status

echo | tee -a $seq_full
echo '== cond-create -c pmfind' | tee -a $seq_full
$sudo pmlogctl cond-create -c pmfind localhost | _filter
echo '== status' | tee -a $seq_full
pmlogctl status | _filter_status
$sudo pmlogctl -c pmfind destroy localhost | _filter

echo | tee -a $seq_full
echo "== cond-create -c pmfind -i $seq-foo" | tee -a $seq_full
$sudo pmlogctl -i $seq-foo cond-create -c pmfind localhost | _filter
echo '== status' | tee -a $seq_full
pmlogctl -V status 2>&1 | _filter_status

# success, all done
status=0
exit
